package com.mrx.xfriendship.controller;

import com.mrx.commons.annotations.XLog;
import com.mrx.commons.annotations.XRateLimit;
import com.mrx.commons.exception.BusinessException;
import com.mrx.commons.util.asserts.Asserts;
import com.mrx.xfriendship.model.dto.Result;
import com.mrx.xfriendship.model.vo.PostVO;
import com.mrx.xfriendship.service.IMailService;
import com.mrx.xfriendship.service.IUserService;
import com.mrx.xfriendship.util.SecurityUtil;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * @author Mr.X
 * @since 2023-02-12 18:41
 */
@XLog
@RestController
@RequestMapping("/mail")
public class MailController {

    @DubboReference
    private IMailService mailService;

    @DubboReference
    private IUserService userService;

    /**
     * 发送 绑定邮箱确认邮件 接口
     *
     * @param toMail 邮箱地址
     */
    @XRateLimit(600)
    @GetMapping("/request")
    public Result<String> request(@RequestParam String toMail) {
        String userName = SecurityUtil.getCurrentUserName();
        mailService.sendBindMail(toMail, userName);
        return Result.success("验证邮件已发送");
    }

    /**
     * 确认绑定 接口
     *
     * @param code 邮箱里的验证码
     */
    @GetMapping("/confirm")
    public Result<String> confirmBind(@RequestParam String code) {
        try {
            userService.bindMail(SecurityUtil.getCurrentUserName(), code);
        } catch (Exception e) {
            if (e instanceof BusinessException) throw e;
            throw Asserts.BIND_MAIL_FAILED.getException();
        }
        return Result.success("绑定邮箱成功");
    }

    /**
     * 发送 找回密码验证码邮件 接口, 不推荐使用, 已废弃<br/>
     * 推荐使用 {@link #recoveryWithEmail(String)}
     *
     * @param userName 要找回密码的用户
     */
    @Deprecated
    @XRateLimit(600)
    @GetMapping("/recovery")
    public Result<String> recovery(HttpServletRequest request, @RequestParam String userName) {
        String server = request.getServerName() + (request.getServerPort() == 80 ? "" : ":" + request.getServerPort());
        mailService.sendRecoveryMail(server, userName);
        return Result.success("验证邮件已发送");
    }

    /**
     * 确认验证码并执行重置密码 接口, 不推荐使用<br/>
     * 推荐使用 {@link #recoveryWithEmailConfirm(String, String)}
     *
     * @param userName 要重置密码的用户名
     * @param code     邮箱验证码
     */
    @Deprecated
    @XRateLimit(600)
    @GetMapping("/recoveryConfirm")
    public Result<String> recoveryConfirm(@RequestParam String userName, @RequestParam String code) {
        mailService.recoveryConfirm(userName, code);
        return Result.success("密码重置成功");
    }

    /**
     * 发送 找回密码验证码邮件 接口
     *
     * @param mail 邮箱号
     */
    @XRateLimit(600)
    @GetMapping("/recoveryWithEmail")
    public Result<String> recoveryWithEmail(@RequestParam String mail) {
        mailService.sendRecoveryMail(mail);
        return Result.success("验证邮件已发送");
    }

    /**
     * 确认验证码并执行重置密码 接口
     *
     * @param mail 要重置密码的邮箱
     * @param code 邮箱验证码
     */
    @GetMapping("/recoveryWithEmailConfirm")
    public Result<String> recoveryWithEmailConfirm(@RequestParam String mail, String code) {
        mailService.recoveryWithEmailConfirm(mail, code);
        return Result.success("密码重置成功");
    }

    /**
     * 发送 反馈邮件给管理员 接口
     */
    @PostMapping("/feedback")
    public Result<String> feedback(@RequestBody PostVO postVO) {
        mailService.sendFeedbackMail(postVO);
        return Result.success();
    }

}
